<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
        <style>
            * {
                margin: 0;
                padding: 0;
            }

            #menu ul li {
                list-style: none;
                height: 45px;
                width: 200px;
                overflow: hidden;
                background-color: pink;
                color: white;
            }

            #menu ul li > span {
                height: 45px;
                line-height: 45px;
            }

            #menu > ul > li > ul > li {
                background-color: green;
            }
        </style>
    </head>

    <body>
        <div id="menu">
            <!-- <ul>
            <li>
                <span>首页</span>
                <ul>
                    <li><span>菜单1</span></li>
                    <li><span>菜单2</span></li>
                    <li><span>菜单3</span></li>
                </ul>
            </li>
        </ul>
        <ul>
            <li>
                <span>博客</span>
                <ul>
                    <li><span>菜单1</span></li>
                    <li><span>菜单2</span></li>
                    <li><span>菜单3</span></li>
                </ul>
            </li>
        </ul> -->
        </div>
        <script>
            const data = [
                { name: "首页", children: [{ name: "1" }] },
                {
                    name: "博客",
                    children: [
                        { name: "菜单一" },
                        { name: "菜单二" },
                        { name: "菜单三" },
                    ],
                },
                {
                    name: "技术支持",
                    children: [
                        { name: "菜单一" },
                        { name: "菜单二" },
                        { name: "菜单三" },
                        { name: "菜单四" },
                    ],
                },
                { name: "操作" },
                { name: "帮组" },
                { name: "新闻" },
            ];
            let menu = document.getElementById("menu");
            function render() {
                for (let i = 0; i < data.length; i++) {
                    const ul = document.createElement("ul");
                    const li = document.createElement("li");
                    li.innerHTML = `<span>${data[i].name}</span>`;
                    li.onclick = function () {
                        openMenu(li);
                    };
                    menu.appendChild(ul);
                    ul.appendChild(li);
                    if (data[i].children !== undefined) {
                    renderSubMenu(li, data[i].children);
                }
                }
                
            }
            function renderSubMenu(dom, children) {
                for (let i = 0; i < children.length; i++) {
                    const ul = document.createElement("ul");
                    const li = document.createElement("li");
                    
                    ul.appendChild(li);
                    dom.appendChild(ul);
                    li.innerHTML = `<span>${children[i].name}</span>`;
                }
            }
            function openMenu(li) {
                const subMenuLiNum = li.querySelectorAll("li").length;
                const currentHeight = getStyleValue(li, "height");
                if (currentHeight === 45) {
                    /* 
        展开菜单的高度
        */
                    const height = (subMenuLiNum + 1) * 45 + "px";

                    li.style.height = height;
                } else {
                    // 关闭
                    li.style.height = "45px";
                }
            }
            function getStyleValue(dom, attr) {
                return parseFloat(getComputedStyle(dom)[attr]);
            }
            render();
        </script>
    </body>
</html>
